package com.tuoyu.peopletest.utils;

public class Utils {
    private final static String hexString="0123456789abcdef";
    private final static String hexStringUpper="0123456789ABCDEF";
    public static String byte2hexString(byte data) {
        int high=(data&0xf0)/16;
        int low=data&0x0f;
        return "0x"+hexString.substring(high,high+1)+hexString.substring(low,low+1);
    }
    public static String byte2hexStringWithPrefix(byte data) {
        return "0x"+byte2hexString(data);
    }
    public static String byte2hexUpperString(byte data) {
        int high=(data&0xf0)/16;
        int low=data&0x0f;
        return "0x"+hexString.substring(high,high+1)+hexString.substring(low,low+1);
    }
    public static String byte2hexUpperStringWithPrefix(byte data) {
        return "0x"+byte2hexUpperString(data);
    }
    public static byte hexString2byte(String str) {
        str=str.toLowerCase();
        if(str.indexOf("0x")==0)
            str=str.substring(2);
        byte data=0;
        if(str.length()==0) //空字符串
            return data;
        data |= hexString.indexOf(str.charAt(0));
        if(str.length()==1) //仅有一个字符
            return data;
        data=(byte)((data*16)&0xF0);
        data |= hexString.indexOf(str.charAt(1))&0x0F;
        return data;
    }
}
